package com.fl.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Properties;
import java.util.Scanner;

public class LicenseUtil {

	public static String getSerialNumber() throws Exception {

		Properties prop = System.getProperties();
		String os = prop.getProperty("os.name").toLowerCase();
		if (os.startsWith("windows")) {
			// windows
			return getWindowsSerialNumber();
		} else if (os.startsWith("linux")) {
			// linux
			return getLinuxSerialNumber();
		}else if (os.startsWith("aix")) {
			// aix
			return getAixSerialNumber();
		}else if (os.startsWith("mac")) {
			// mac
			return getMacSerialNumber();
		}else if (os.startsWith("hp")) {
			// hp unix
			return getHpSerialNumber();
		}else{
			throw new Exception("OS "+os+" is not suport !");
		}

	}

	private static String getWindowsSerialNumber() {

		String sn = "";

		OutputStream os = null;
		InputStream is = null;
		Runtime runtime = Runtime.getRuntime();
		Process process = null;
		try {
			process = runtime.exec(new String[] { "wmic", "bios", "get",
					"serialnumber" });
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		os = process.getOutputStream();
		is = process.getInputStream();
		try {
			os.close();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		Scanner sc = new Scanner(is);
		try {
			while (sc.hasNext()) {
				String next = sc.next();
				if ("SerialNumber".equals(next)) {
					sn = sc.next().trim();
					break;
				}
			}
		} finally {
			try {
				sc.close();
				is.close();
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		if (sn == null) {
			throw new RuntimeException("Cannot find computer SN");
		}
		return sn;
	}

	private static String getAixSerialNumber() {
		//#uname -u
		// prtconf |grep "Machine Serial Number"
		//lsconf|more
		//Machine Serial Number: XXXXXX
		String sn = null;

		OutputStream os = null;
		InputStream is = null;
		Runtime runtime = Runtime.getRuntime();
		Process process = null;
		try {
			process = runtime
					.exec(new String[] { "lsconf" });
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		os = process.getOutputStream();
		is = process.getInputStream();
		try {
			os.close();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		String line = null;
		String marker = "Machine Serial Number:";
		try {
			while ((line = br.readLine()) != null) {
				if (line.indexOf(marker) != -1) {
					sn = line.split(marker)[1].trim();
					break;
				}
			}
		} catch (IOException e) {
			throw new RuntimeException(e);
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		if (sn == null) {
			throw new RuntimeException("Cannot find computer SN");
		}
		return sn;
	}
	
	private static String getHpSerialNumber() {
		//machinfo
		// Machine serial number:  SGH4843043
		String sn = null;

		OutputStream os = null;
		InputStream is = null;
		Runtime runtime = Runtime.getRuntime();
		Process process = null;
		try {
			process = runtime
					.exec(new String[] { "machinfo" });
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		os = process.getOutputStream();
		is = process.getInputStream();
		try {
			os.close();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		String line = null;
		String marker = "Machine serial number:";
		try {
			while ((line = br.readLine()) != null) {
				if (line.indexOf(marker) != -1) {
					sn = line.split(marker)[1].trim();
					break;
				}
			}
		} catch (IOException e) {
			throw new RuntimeException(e);
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		if (sn == null) {
			throw new RuntimeException("Cannot find computer SN");
		}
		return sn;
	}

	private static String getLinuxSerialNumber() {

		String sn = null;

		OutputStream os = null;
		InputStream is = null;
		Runtime runtime = Runtime.getRuntime();
		Process process = null;
		try {
			process = runtime
					.exec(new String[] { "dmidecode", "-t", "system" });
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		os = process.getOutputStream();
		is = process.getInputStream();
		try {
			os.close();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		String line = null;
		String marker = "Serial Number:";
		try {
			while ((line = br.readLine()) != null) {
				if (line.indexOf(marker) != -1) {
					sn = line.split(marker)[1].trim();
					break;
				}
			}
		} catch (IOException e) {
			throw new RuntimeException(e);
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		if (sn == null) {
			throw new RuntimeException("Cannot find computer SN");
		}
		return sn;
	}

	private static String getMacSerialNumber() {

		String sn = null;

		OutputStream os = null;
		InputStream is = null;
		Runtime runtime = Runtime.getRuntime();
		Process process = null;
		try {
			process = runtime.exec(new String[] { "/usr/sbin/system_profiler",
					"SPHardwareDataType" });
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		os = process.getOutputStream();
		is = process.getInputStream();
		try {
			os.close();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		String line = null;
		String marker = "Serial Number:";
		try {
			while ((line = br.readLine()) != null) {
				if (line.indexOf(marker) != -1) {
					sn = line.split(marker)[1].trim();
					break;
				}
			}
		} catch (IOException e) {
			throw new RuntimeException(e);
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		if (sn == null) {
			throw new RuntimeException("Cannot find computer SN");
		}
		return sn;
	}

	public static void main(String[] args) {
		try {
			System.out.println(getSerialNumber());
		} catch (Exception exception) {
			exception.printStackTrace();
		}
	}

}
